<?php

namespace app\api\controller;

use app\api\service\Message;
use app\common\library\wechat\WxPay;
use app\common\model\Reservation;
use think\Db;

/**
 * 支付成功异步通知接口
 * Class Notify
 * @package app\api\controller
 */
class Notify
{
    /**
     * 支付成功异步通知
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function order()
    {
        // 微信支付组件：验证异步通知
        $WxPay = new WxPay();
        $WxPay->notify();
    }

    public function lesson(){
        $where['start_time'] = [['EGT',date('Y-m-d H:i:s')],['ELT',date('Y-m-d H:i:s',time()+1*60*60)],'and'];
        $list = Reservation::where($where)->order('start_time asc')->select()->toArray();
        foreach ($list as $k=>$v){
            //检查是否已经发过了
            $search['coach_id'] = $v['coach_id'];
            $search['course_id'] = $v['course_id'];
            $search['start_time'] = $v['start_time'];
            $check = Db::name('gz_push')->where($search)->find();
            if (!$check){
                //获取教练的gzopenid
                $user_id = Db::name('coach')->where('coach_id',$v['coach_id'])->value('user_id');
                $coach_user = Db::name('user')->where('user_id',$user_id)->find();
                if ($coach_user && $coach_user['gz_open_id'] != ''){
                    //推送
                    $course = Db::name('course')->find($v['course_id']);
                    $params['gz_open_id'] = $coach_user['gz_open_id'];
                    $params['course_name'] = $course['course_name'];
                    $params['start_time'] = $v['start_time'];
                    $message = new Message();
                    $message->lesson($params);
                    //插入表
                    $add['coach_id'] = $v['coach_id'];
                    $add['course_id'] = $v['course_id'];
                    $add['start_time'] = $v['start_time'];
                    $add['wxapp_id'] = $v['wxapp_id'];
                    $add['create_time'] = time();
                    Db::name('gz_push')->insert($add);
                }

            }
        }
    }
}
